﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HR_ASP.ViewModel;
using DB.Model;

namespace HR_ASP.Controllers
{
    public class BadanieController : Controller
    {
        //
        // GET: /Stanowisko/

        public ActionResult Index()
        {
            using (var context = new HREntities())
            {               
                return View(context.Badania.Select(s => new BadanieViewModel
                {
                    ID = s.ID_Bad,
                    Nazwa = s.Nazwa, 
                    Opis = s.Opis,
                    DataU = (DateTime)s.DataU,
                    DataM = (DateTime)s.DataM
                }).ToArray()
                );               
            }
        }

        public ActionResult Create()
        {
            return View("Create");
        }

        [HttpPost]
        public ActionResult Create(BadanieViewModel bd)
        {
            using (var context = new HREntities())
            {
                context.Badania.AddObject(bd.GetModel(new Badanie()));
                context.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            using (var context = new HREntities())
            {
                var st = context.Badania.SingleOrDefault(s => s.ID_Bad == id);
                if (st == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new BadanieViewModel(st));
            }
        }

        [HttpPost]
        public ActionResult Edit(BadanieViewModel bd)
        {
            using (var context = new HREntities())
            {
                var badanieDB = context.Badania.SingleOrDefault(s => s.ID_Bad == bd.ID);
                context.ApplyCurrentValues<Badanie>("Badania", bd.GetModel(badanieDB));
                context.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            using (var context = new HREntities())
            {
                var employee = context.Badania.SingleOrDefault(s => s.ID_Bad == id);
                if (employee != null)
                {
                    context.Badania.DeleteObject(employee);
                    context.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult Details(int id)
        {
            using (var context = new HREntities())
            {
                var st = context.Badania.SingleOrDefault(s => s.ID_Bad == id);
                if (st == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new BadanieViewModel(st));
            }
        }

    }
}
